let fetch = require("../../utils/fetch");
let util = require("../../utils/util");
let mta = require('../../utils/mta_analysis.js');
let app = getApp();
Page({
    data: {
        imageUrl: app.globalData.imageUrl,
        winHeight: "", //窗口高度
        currentTab: 0, //预设当前项的值
        shopCode: "",
        currentSwiper: 0,
        allList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        unpayList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        succList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false
        }
    },


    // 滚动切换标签样式
    switchTab: function(e) {
        if (e.detail.source != 'autoplay') {
            console.log(1)

            this.setData({
                currentTab: e.detail.current
            });
            this.judgeSwitch(e.detail.current)
        }

        // this.checkCor();
    },

    judgeSwitch: function(current) {
        this.setNavigationBarTitle(current)
        if (current == 0 && this.data.allList.list.length == 0) {
            this.loadMore(100, "allList")
        } else if (current == 1 && this.data.unpayList.list.length == 0) {
            this.loadMore(0, "unpayList")
        } else if (current == 2 && this.data.succList.list.length == 0) {
            this.loadMore(2, "succList")
        }
    },
    // 点击标题切换当前页时改变样式
    switchNav: function(e) {
        // wx.showToast({
        //     mask: true,
        //     title: '加载中',
        //     icon: 'loading',
        //     duration: 500
        // })
        var cur = e.currentTarget.dataset.current;
        console.log(e)
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur,
                currentSwiper: cur
            })
        }
        this.judgeSwitch(cur)
    },
    setNavigationBarTitle: function(current) {
        if (current == 0) {
            wx.setNavigationBarTitle({
                title: '全部订单'
            })
        } else if (current == 1) {

            wx.setNavigationBarTitle({
                title: '待付款订单'
            })
        } else if (current == 2) {
            wx.setNavigationBarTitle({
                title: '已完成订单'
            })
        }
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function() {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    onLoad: function(options) {

        mta.Page.init();
        mta.Event.stat("smg_dingdan", {});
        console.log('onLoad')


        var that = this;
        if (!options.current) {
            options.current = 0;
        }
        //  高度自适应
        this.heightAuto()
        this.setData({
            currentTab: options.current,
            currentSwiper: options.current,
            shopCode: app.globalData.common.shopCode

        })
        this.judgeSwitch(options.current)

    },
    heightAuto: function() {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                that.setData({
                    winHeight: calc
                });
            }
        })

    },
    lower: function(e) {
        console.log('lower', e.currentTarget.dataset.list)
        this.loadMore(e.currentTarget.dataset.status, e.currentTarget.dataset.list)

    },
    loadMore: function(orderStatus, lists) {
        if (this.data[lists].isLast || this.data[lists].isLoading) {
            return
        } else {
            this.getOrderList(orderStatus, this.data[lists].curPage, lists)

        }

    },
    onShowMethod: function(orderStatus, pageNum, lists) {

        this.getOrderList(orderStatus, pageNum, lists)

    },
    getOrderList: function(orderStatus, pageNum, lists) {
        // fetch.loadingText()
        this.setData({
            [lists]: {
                isLast: this.data[lists].isLast,
                curPage: this.data[lists].curPage,
                list: this.data[lists].list,
                isLoading: true
            },
        })
        fetch.getOrders(orderStatus, pageNum).then((response) => {
            fetch.hideLoadingText()
            const res = response.data;
            if (res.code == 200) {

                //let list = this.data[lists].list.concat(res.obj.result)
                let list = res.obj.result;
               // console.log(res.obj.result)
                for (var i = 0; i < list.length; i++) {
                    if (list[i].reserveOrder) {
                        list[i].urlSrt = '/pages/reserveOrder/reserveOrder?code=' + list[i].orderCode,
                            list[i].payUrlSrt = '/pages/reservewaitPay/reservewaitPay?code=' + list[i].orderCode

                    } else {
                        list[i].urlSrt = '/pages/detail/detail?code=' + list[i].orderCode,
                            list[i].payUrlSrt = '/pages/pay/pay?code=' + list[i].orderCode
                    }

                }
                //console.log(list);
                this.setData({
                    [lists]: {
                        isLast: res.obj.lastPage,
                        curPage: this.data[lists].curPage + 1,
                        list: list,
                        isLoading: false
                    },
                })
            }
        }).catch((err) => {
            fetch.hideLoadingText()
            fetch.showMyToast(this, err.data.des)
        })
    },
    goToPay: function() {
        mta.Event.stat("smg_btdaifukuan", {})
    },

    onShow: function() {
        // 判断当从支付页跳转后，刷新订单
        // if (app.globalData.common.selfPay) {
        
        
        this.initData()
             this.onShowMethod(100, 1, 'allList')
        //     app.globalData.common.selfPay = null;
        // }
    },
    onPullDownRefresh() {
        this.initData();
        this.onShowMethod(100, 1, 'allList');
    },
    initData: function() {
        this.setData({
            currentTab: 0, //预设当前项的值
            allList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false

            },
            unpayList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false

            },
            succList: {
                isLast: false,
                curPage: 1,
                list: [],
                isLoading: false
            }
        })
    },

    onHide: function() {

    },
    onUnload: function() {
        console.log('onUnload')
    },

    checkNetwork: function() {
        fetch.NetworkType().then((res) => {
            console.log(res);
            const networkType = res.networkType;
            console.log(networkType)
            if (networkType == '2g' || networkType == 'none' || networkType == 'unknown') {
                const self = this;
                fetch.showMyToast(self, `当前网络状况为：${networkType}，可能会影响使用效果`)
            }
        })
    },
    callOwers: function(e) {
        // console.log(e.currentTarget.dataset.code)
        var shopCode = e.currentTarget.dataset.code;
        var _self = this;

        wx.request({
            url: getApp().globalData.baseUrl + "/applet/reserve/getShopkeeperMobile.do",
            data: {

                shopCode: shopCode
            },
            header: getApp().globalData.header,
            success: function(res) {
                console.log(res);
                if (res.data.code == 200) {
                    wx.showModal({
                        title: '拨打店主电话',
                        content: res.data.obj,
                        success(ress) {
                            if (ress.confirm) {
                                wx.makePhoneCall({
                                    phoneNumber: res.data.obj
                                })
                            } else if (ress.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })

                } else {
                    wx.showModal({
                        title: '拨打客服电话',
                        content: '4000660158',
                        success(res) {
                            if (res.confirm) {
                                wx.makePhoneCall({
                                    phoneNumber: '4000660158'
                                })
                            } else if (res.cancel) {
                                console.log('用户点击取消')
                            }
                        }
                    })

                }
            }
        })



    }

})